
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "log.h"                // LOG file 
#define LOG     LOG_INFO0

#include "xapp_timer.h"
#include "xapp_clock.h"
#include "xapp_localize.h"

enum tagTimerSelect
{
    tagTimerSelect0 = 0,
    tagTimerSelect1,
    tagTimerSelect2,
    tagTimerSelect3,
    tagTimerSelect4,
    tagTimerSelect5,
    tagTimerSelect6,
    tagTimerSelectCustom,
    tagTimerSelectReturn,
    tagTimerSelectDoNotCare,
};

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag );

#define ITEM_WIDTH      (GUI_LCD_RES_H / 2)
#define ITEM_HEIGHT     (GUI_LCD_RES_V / 2)

#define OFFS   2

const uint8_t MINUTES_DEF[] = { 1, 2, 5, 10, 20, 30, 60 };

const static ui_widgets_type_t wglTimerSelect/*1. modify name*/[] = 
{
    // background 
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),

    [1] = uiTextbox( 0, 20, GUI_LCD_RES_H, 29, COLOR_YELLOW1, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_BACKCOLOR, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    [2] = uiListBoxTouch( 0, 50, GUI_LCD_RES_H, GUI_LCD_RES_V - 50, 24, 0, 0, false, tagTimerSelectDoNotCare ),
    // 1 minutes 
    
    [1 + OFFS] = uiStackTouch( 0, ITEM_HEIGHT * 0, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect0 ), 
    [2 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [3 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "1", 1 ),

    // 2 minutes
    [4 + OFFS] = uiStackTouch( ITEM_WIDTH, ITEM_HEIGHT * 0, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect1 ),
    [5 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [6 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "2", 1 ),

    // 5 minutes
    [7 + OFFS] = uiStackTouch( 0, ITEM_HEIGHT * 1, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect2 ),
    [8 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [9 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "5", 1 ),

    // 10 minutes
    [10 + OFFS] = uiStackTouch( ITEM_WIDTH, ITEM_HEIGHT * 1, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect3 ),
    [11 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [12 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "10", 2 ),

    // 20 minutes
    [13 + OFFS] = uiStackTouch( 0, ITEM_HEIGHT * 2, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect4 ),
    [14 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [15 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "20", 2 ),

    // 30 minutes
    [16 + OFFS] = uiStackTouch( ITEM_WIDTH, ITEM_HEIGHT * 2, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect5 ),
    [17 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [18 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "30", 2 ),

    // 60 minutes
    [19 + OFFS] = uiStackTouch( 0, ITEM_HEIGHT * 3, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelect6 ),
    [20 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [21 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "60", 2 ),

    // + extra minutes 
    [22 + OFFS] = uiStackTouch( ITEM_WIDTH, ITEM_HEIGHT * 3, ITEM_WIDTH, ITEM_HEIGHT, 2, tagTimerSelectCustom ),
    [23 + OFFS] = uiPictureBox( IMG_Watchface_Circle_BG_104_104_01, 0, 25, 25, 104, 104 ),
    [24 + OFFS] = uiLabel( 0, 0, ITEM_WIDTH, ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "+", 1 ),

};


GUI_VIEW_DEF_NR( viewTools_TimerSelect ) = 
{
    // name 
    .name = "Tools+TimerSelect",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglTimerSelect /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT( wglTimerSelect ), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC
};

static gui_fling_t*  mp_select_fling;
static int16_t      roll_last_total;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 1000;
    menu->lifetime = 0; 

    mp_select_fling = gui_fling_get();
    gui_fling_init_t init_obj = GUI_FLING_CONFIG( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, true, ITEM_HEIGHT * (-3), 0, ITEM_HEIGHT, 100, NULL );
    gui_fling_init( mp_select_fling, &init_obj );
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
   ui_property_t prop;
   ui_widgets_type_t * widgets = menu->widgets;

   string_t const *s;
   
   if( IS_REQ_SRC_CREATE( menu->source ) )
   {
       for(int i = 0; i < 8; i ++)
        {
            gui_widget_update_prop( &widgets[3 * i + 5], NULL );
        }

        s = xapp_local_text_item_get( TEXT_TAG_TOOLS_TIMER );
        PROP_ASSIGN_STRING( prop, s );
        PROP_UPDATE_AT_NO( 1 ); 
        
        if( menu->source == GUI_VIEW_REQ_SRC_CREATE_RETURN )
        {
            mp_select_fling->roll_offset_total = roll_last_total;
        }
   }
   
   // 更新listpanel 偏移量
    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_FLING )
    {
        PROP_ASSIGN_LISTBOX_OFFSET( prop, mp_select_fling->roll_offset_total );
        PROP_UPDATE_AT_NO( 2 ); 
    }
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_DOWN:
        gui_fling_on_touch_press( mp_select_fling );
        break;
        case TP_ALGO_EVT_TYPE_UP:
        gui_fling_on_touch_release( mp_select_fling );
        break;

        case TP_ALGO_EVT_TYPE_CLICK:
        GUI_WIDGET_LIST_CHECK_TOUCH( menu->widgets, menu->cnt_of_widgets, 0, 0,  touch->x, touch->y, local_view_event_handler); 
        break;
        
        case TP_ALGO_EVT_TYPE_HOLD:
        if( touch->deltaY != 0)
        {
            gui_fling_set_roll( mp_select_fling, touch->deltaY );
        }
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_D:
        gui_fling_switch_item_extend( mp_select_fling, 1, false, GUI_FLING_SWITCH_AVG );
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_U:
        gui_fling_switch_item_extend( mp_select_fling, 1, true, GUI_FLING_SWITCH_AVG );
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_L:

        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_R:
        local_view_event_handler( tagTimerSelectReturn );
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t * kb = (kb_evt_t*)pvParameters;
    
    switch( kb->type ) 
    {
        case KB_EVT_CLICK:           
        case KB_EVT_SHORT_HOLD_READY:
        local_view_event_handler( tagTimerSelectReturn );
        break;

        default:
        break;
    }
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    (void)pvParameters;
    (void)menu;
}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
static void local_view_event_handler( uint32_t tag )
{
    
    switch( tag )
    {
        case tagTimerSelect0:
        case tagTimerSelect1:
        case tagTimerSelect2:
        case tagTimerSelect3:
        case tagTimerSelect4:
        case tagTimerSelect5:
        case tagTimerSelect6:
        xapp_timer_set_time( 0, MINUTES_DEF[tag] );
        xapp_timer_counting_start(); // start counting 
        gui_view_create_sub( &viewTools_TimerSelect, &viewTools_TimerElapsed );
        break;

        case tagTimerSelectCustom:
        gui_view_create_sub( &viewTools_TimerSelect, &viewTools_TimerCustom );
        break;

        case tagTimerSelectReturn: // 返回功能界面 
        gui_view_create_return( &viewAppsList ); 
        break;
    }
}
